package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.cookboy.CookBoyConfig;
import com.yanqu.road.entity.activity.cookboy.CookBoyFoodConfig;
import com.yanqu.road.entity.activity.cookboy.process2.CookBoy2DishConfig;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.activity.eActivityCookBoyType;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.cookboyactivity.CookBoyActivityProto;
import com.yanqu.road.server.logic.activity.relation.RelationActivityData;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;

import java.util.List;
import java.util.Map;

public class CookBoyActivityPb {

    private static CookBoyActivityProto.CookBoyActivityTempMsg.Builder parseCookBoyActivityTempMsg(ActivityInfo activityInfo, String language){
        if(null == activityInfo){
            return null;
        }
        CookBoyActivityProto.CookBoyActivityTempMsg.Builder msg = CookBoyActivityProto.CookBoyActivityTempMsg.newBuilder();
        msg.setActivityId(activityInfo.getActivityId());
        msg.setChildType(activityInfo.getChildType());
        msg.setActivityName(ServerLanguageMgr.getContent(activityInfo.getActivityName(), language));
        msg.setDescription(ServerLanguageMgr.getContent(activityInfo.getActivityDesc(), language));
        msg.setBeginShowTime(activityInfo.getBeginShowTime());
        msg.setEndShowTime(activityInfo.getEndShowTime());
        msg.setBeginTime(activityInfo.getBeginTime());
        msg.setEndTime(activityInfo.getEndTime());
        msg.setIcon(activityInfo.getParam2());
        msg.setPic(activityInfo.getParam3());
        msg.setParam4(activityInfo.getParam4());
        msg.setParam5(activityInfo.getParam5());
        msg.setParam6(activityInfo.getParam6());
        msg.addAllServerList(activityInfo.getServerIdList());
        msg.addAllServerList2(activityInfo.getServerIdList2());
        return msg;
    }

    public static CookBoyActivityProto.CookBoyActivityMsg.Builder parseCookBoyActivityMsg(ActivityInfo activityInfo,
            Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap, Map<Integer, MallInfo> activityMallInfoMap, String language){
        if(null == activityInfo) {
            return null;
        }
        CookBoyActivityProto.CookBoyActivityMsg.Builder resultMsg = CookBoyActivityProto.CookBoyActivityMsg.newBuilder();
        //活动
        CookBoyActivityProto.CookBoyActivityTempMsg.Builder activityMsg = parseCookBoyActivityTempMsg(activityInfo, language);
        if (null != activityMsg) {
            resultMsg.setActivityInfo(activityMsg);
        }
        //活动条件
        if(null != activityConditionInfoMap) {
            for (List<ActivityConditionInfo> conditionInfoList : activityConditionInfoMap.values()) {
                for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                    CommonActivityProto.CommonActivityConditionTempMsg.Builder conditionMsg = CommonActivityPb.parseCommonActivityConditionTempMsg(conditionInfo, language);
                    if (null != conditionMsg) {
                        resultMsg.addActivityConditionInfoList(conditionMsg);
                    }
                }
            }
        }
        //活动商品
        if(null != activityMallInfoMap) {
            for (MallInfo mallInfo : activityMallInfoMap.values()) {
                CommonProto.MallTempMsg.Builder mallMsg = CommonPb.parseMallTempMsg(mallInfo, language);
                if (null != mallMsg) {
                    resultMsg.addActivityMallInfoList(mallMsg);
                }
            }
        }
        Map<String, CookBoyConfig> configMap = CookBoyActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        for(CookBoyConfig config : configMap.values()){
            CookBoyActivityProto.CookBoyActivityConfigTemp.Builder temp = CookBoyActivityProto.CookBoyActivityConfigTemp.newBuilder();
            temp.setId(config.getId());
            temp.setParam(config.getParam());
            temp.setValue(config.getValue());
            resultMsg.addConfigList(temp);
        }
        //酒家经营
        if(activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()){
            Map<Integer, CookBoyFoodConfig> foodMap = CookBoyActivityMgr.getActivityFoodConfigMap(activityInfo.getActivityId());
            for(CookBoyFoodConfig food : foodMap.values()){
                CookBoyActivityProto.CookBoyActivityFoodTemp.Builder temp = CookBoyActivityProto.CookBoyActivityFoodTemp.newBuilder();
                temp.setFoodId(food.getCookingId());
                temp.setIndex(food.getCookingOrder());
                temp.setMax(food.getUnlockCondition());
                temp.setAdd(food.getAddScore());
                resultMsg.addFoodList(temp);
            }
        }else if(activityInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()){
            Map<Integer, CookBoy2DishConfig> dishMap = CookBoyActivityMgr.getActivityDishConfigMap(activityInfo.getActivityId());
            for(CookBoy2DishConfig dish : dishMap.values()){
                CookBoyActivityProto.CookBoyActivityDishTemp.Builder temp = CookBoyActivityProto.CookBoyActivityDishTemp.newBuilder();
                temp.setId(dish.getId());
                temp.setQuality(dish.getQuality());
                temp.setMenu(dish.getMenu());
                temp.setSeat(dish.getSeat());
                temp.setScore(dish.getScore());
                temp.setWeight(dish.getWeight());
                resultMsg.addDishList(temp);
            }
        }else if(activityInfo.getChildType() == eActivityCookBoyType.Process3.getChildType()){

        }
        return resultMsg;
    }

    public static CookBoyActivityProto.CookBoyActivityDetailMsg.Builder parseCookBoyActivityDetailMsg(Map<Integer,Map<Integer, RelationActivityData>> activityDataMap,
                                                                                                   List<ActivityInfo> activityInfoList,
                                                                                                   Map<Integer,Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoMap,
                                                                                                   Map<Integer,Map<Integer, MallInfo>> activityMallInfoMap, String language){
        CookBoyActivityProto.CookBoyActivityDetailMsg.Builder resultMsg = CookBoyActivityProto.CookBoyActivityDetailMsg.newBuilder();
        for(ActivityInfo activityInfo : activityInfoList){
            CookBoyActivityProto.CookBoyActivityMsg.Builder activityMsg =
                    parseCookBoyActivityMsg(activityInfo, activityConditionInfoMap.get(activityInfo.getActivityId()), activityMallInfoMap.get(activityInfo.getActivityId()), language);
            if (null != activityMsg) {
                resultMsg.addActivityInfo(activityMsg);
            }
        }

        //活动数据
        CommonActivityProto.CommonActivityDataMsg.Builder activityDataMsg = parseCommonActivityDataMsg(activityDataMap);
        if(null != activityDataMsg){
            resultMsg.setActivityData(activityDataMsg);
        }

        return resultMsg;
    }

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(Map<Integer, Map<Integer, RelationActivityData>> activityDataMap){
        if(null == activityDataMap){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (Map<Integer, RelationActivityData> tempMap : activityDataMap.values()) {
            for (RelationActivityData activityData : tempMap.values()) {
                for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
                    CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
                    if (null != conditionDataMsg) {
                        msg.addConditionDataList(conditionDataMsg);
                    }
                }
            }
        }

        return msg;
    }


    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(RelationActivityData activityData){
        if(null == activityData){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
            CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
            if (null != conditionDataMsg) {
                msg.addConditionDataList(conditionDataMsg);
            }
        }
        return msg;
    }

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataListMsg(List<RelationActivityData> activityDataList){
        if(0 == activityDataList.size()){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for(RelationActivityData activityData : activityDataList){
            for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
                CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
                if (null != conditionDataMsg) {
                    msg.addConditionDataList(conditionDataMsg);
                }
            }
        }
        return msg;
    }
}
